﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace ClassLibrary1.entity
{
    [XmlRoot("insurance")]
    public class Insurance:BaseEntity
    {
        [XmlElement("cargo")]
        public Cargo cargo { get; set; }

        [XmlElement("pay")]
        public int pay { get; set; }

        public Insurance()
        {
            int id = 1;

            while (true)
            {
                var entity = FindById(id.ToString());
                if (entity == null)
                {
                    this.id = id;
                    return;
                }
                id++;
            }
        }

        public Insurance(string id)
        {
            if (Insurance.FindById(id) != null) return;
            int.TryParse(id, out this.id);
        }

        public Insurance(string[]mas)
        {
            this.id = new Insurance(mas[0]).id;
                Upd(mas.Skip(1).ToArray());
        }

        public static Insurance FindById(string id)
        {
            return (Insurance)Data.entity.FirstOrDefault(item => item.GetType() == typeof(Insurance) && item.id == int.Parse(id));
        }

        public static List<BaseEntity> FindAll()
        {
            return Data.entity.FindAll(item => item.GetType() == typeof(Insurance));
        }

        public override void Upd(string[]mas)
        {
            try { this.pay = Convert.ToInt32(mas[0]); }
            catch (Exception) { }
            try { this.cargo = Cargo.FindById(mas[1]); }
            catch (Exception) { }
        }

        public bool Equals(Booking a)
        {
            if (this.id.Equals(a.id)) return true;
            return false;
        }

        public override string Write()
        {
            string s = "insurance" + " " + this.id;
            s += " ";
            if (this.pay != 0) s += this.pay.ToString();
            s += " ";
            if (this.cargo != null) s += this.cargo.id.ToString();
            return s;
        }
    }
}
